package com.ieslab.powergrid.demosvr.utils;

import com.ieslab.powergrid.demosvr.entity.MyException;
import com.ieslab.powergrid.demosvr.entity.Result;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap;
import java.util.Map;

/** <p>Title: GlobleExceptionHandler </p>
 * <p>Description: 全局异常捕捉处理</p>
 *
 * @author houpeibin
 * @date 2020-2-20 下午7:15:30
 * @version V1.0
 */
@ControllerAdvice
public class GlobleExceptionHandler {

    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public Result errorHandler(Exception ex) {
        //判断异常的类型,返回不一样的返回值
        if(ex instanceof MissingServletRequestParameterException){
            return Result.error(400, 0, "全局异常捕捉：缺少必需参数："
                    +((MissingServletRequestParameterException) ex).getParameterName());
        }
        else if(ex instanceof MyException){
           return Result.error(400, 0, "全局异常捕捉：这是自定义异常");
        }
        return Result.error(400, 0, "全局异常捕捉：未知异常");
    }
}
